/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.service.merchant;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.service.BaseService;
import com.neusoft.lohas.modules.ivm.dao.merchant.DeliveryFeeDao;
import com.neusoft.lohas.modules.ivm.entity.merchant.DeliveryFee;
import com.neusoft.lohas.modules.ivm.entity.merchant.Merchant;

/**
 * 配送费Service
 * @author Jackie.Liu
 * @version 2015-04-16
 */
@Component
@Transactional(readOnly = true)
public class DeliveryFeeService extends BaseService {

	@Autowired
	private DeliveryFeeDao deliveryFeeDao;
	
	public DeliveryFee get(String id) {
		return deliveryFeeDao.get(id);
	}
	
	public Page<DeliveryFee> find(Page<DeliveryFee> page, DeliveryFee deliveryFee) {
		DetachedCriteria dc = deliveryFeeDao.createDetachedCriteria();
		dc.add(Restrictions.eq(DeliveryFee.FIELD_DEL_FLAG, DeliveryFee.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return deliveryFeeDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(DeliveryFee deliveryFee) {
		deliveryFeeDao.save(deliveryFee);
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		deliveryFeeDao.deleteById(id);
	}
	
	public DeliveryFee findByMerchantId(String merchantId) {
		DeliveryFee deliveryFee = deliveryFeeDao.findByMerchantId(merchantId);
		if (deliveryFee == null) {
			deliveryFee = new DeliveryFee();
			deliveryFee.setMerchant(new Merchant(merchantId));
		}
		return deliveryFee;
	}
	
}
